﻿namespace Digitalmes.Application.Sys.Posts.Commands;

/// <summary>
/// 创建职位命令。
/// </summary>
public record CreatePosCommand(SysPosDto Pos) : IRequest<IResult<bool>>;

internal sealed class CreatePosCommandHandler(IRepository<SysPos> repo) : IRequestHandler<CreatePosCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(CreatePosCommand request, CancellationToken cancellationToken)
    {
        // 检查编号是否已存在。
        if (await repo.IsAnyAsync(m => m.Code == request.Pos.Code, cancellationToken))
        {
            return Result.Failure("职位编号已存在");
        }

        var pos = request.Pos.Adapt<SysPos>();
        var ok = await repo.InsertAsync(pos, cancellationToken);
        return Result.From(ok);
    }
}
